//
//  InformationSet.m
//  YaoHeAPP
//
//  Created by mac on 15/12/30.
//  Copyright © 2015年 mac. All rights reserved.

//***************************************我的个人账户设置

#import "InformationSet.h"
#import "Masonry.h"
#import "Controller.h"
#import "Define.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "ChangeMobileVC.h"
#import "ChangeNameVC.h"
#import "ChangePositionVC.h"
#import "ChangePassword.h"
@interface InformationSet ()
{
    NSInteger width;
    NSInteger height;
    UIImageView*headImg;
}
@end

@implementation InformationSet

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"InformationSet"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    
    [_tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"InformationSet"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=bgVCcolor;
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"我的账户";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backUp) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    
    //请求数据
    [self updataView];
    
    
    }

-(void)updataView
{
    //创建表格
    [Controller requestWithURL:@"AppUserCenter/PostQueryUserInfo?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            
        NSDictionary*dic=classData.data;
        [_tableView removeFromSuperview];
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, width, height-kNavigationBarHeight) style: UITableViewStyleGrouped];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        //_tableView.scrollEnabled=NO;
        [self.view addSubview:_tableView];
        _message=[[SelfMessage alloc]initWithDic:dic];
        
        [_tableView reloadData];
     }
        else{
            ALERTSHOW(classData.message);
        }
        
    }];



}



//分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//每个分区单元格的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 4;
    }
    return 1;
}

//每个单元格的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            return 66;
        }
        return 46;
    }
    
    return 46;
}

//设置表格要显示的内容
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSArray*array1=@[@"头像",@"手机号:",@"真实姓名:",@"职位:"];
    NSArray*array2=@[@"",_message.regMobile,_message.regName,_message.regTitle];
    if (indexPath.section==0) {
        UITableViewCell*cell1=[[UITableViewCell alloc]init];
        cell1.selectionStyle=UITableViewCellSelectionStyleNone;
        cell1.textLabel.text=array1[indexPath.row];
        cell1.textLabel.font=font16;
        
        if (indexPath.row==0) {
            headImg=[[UIImageView alloc]init];
            headImg.layer.masksToBounds=YES;
            headImg.layer.cornerRadius=25;
            [headImg sd_setImageWithURL:[NSURL URLWithString:_message.logoPath] placeholderImage:[UIImage imageNamed:@"default_head"]];
            [cell1 addSubview:headImg];
//            [headImg mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.right.offset(-15);  make.top.offset(8);
//                make.height.offset(50);    make.width.offset(50);
//            }];
            headImg.frame=CGRectMake(width-15-50, 8, 50, 50);
            
        }
         
        
        //显示后方的详情
        UILabel*mesg=[[UILabel alloc]init];
        mesg.font=font16;
        mesg.text=array2[indexPath.row];
        mesg.textAlignment=NSTextAlignmentRight;
        [cell1 addSubview:mesg];
//        [mesg mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.offset(-15);   make.top.offset(10);
//            make.bottom.offset(-10);
//        }];
        
        mesg.frame=CGRectMake(width-15-120, 10, 120, 26);
        
        return cell1;
    }
    UITableViewCell*cell2=[[UITableViewCell alloc]init];
    cell2.selectionStyle=UITableViewCellSelectionStyleNone;
    cell2.textLabel.text=@"登录密码:";
    cell2.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 0://更换头像
            {
                UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择", nil];
                [sheet showInView:self.view];
            }
                
                break;
            case 1://修改手机号
            {
                ChangeMobileVC*mobileVC=[[ChangeMobileVC alloc]init];
                mobileVC.message=_message;
                [self.navigationController pushViewController:mobileVC animated:YES];
            }
                break;
            case 2://真实姓名（只能修改一次）
            {
                ChangeNameVC*nameVC=[[ChangeNameVC alloc]init];
                nameVC.message=_message;
                [self.navigationController pushViewController:nameVC animated:YES];
            }
                break;
            case 3://职位
            {
                ChangePositionVC*positionVC=[[ChangePositionVC alloc]init];
                positionVC.message=_message;
                [self.navigationController pushViewController:positionVC animated:YES];
            }
                break;
            default:
                break;
        }
    }
    if (indexPath.section==1) {
        switch (indexPath.row) {
            case 0://修改登录密码
            {
                ChangePassword*password=[[ChangePassword alloc]init];
                [self.navigationController pushViewController:password animated:YES];
                
            }
                break;
                
            default:
                break;
        }
    }
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //拍照
    if (buttonIndex== 0) {
        
        BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
        if(!isCamera)
        {
            ALERTSHOW(@"无法调用相机,请检查系统设置");
            return;
        }
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.sourceType= UIImagePickerControllerSourceTypeCamera;
        _imagePickerController.delegate=self;
        //编辑模式
        _imagePickerController.allowsEditing= YES;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
        
    }
    else if (buttonIndex == 1) {
        //从相册选择
        _imagePickerController= [[UIImagePickerController alloc] init];
        //设置picker类型
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置代理
        _imagePickerController.delegate = self;
        //设置图片可以修改
        _imagePickerController.allowsEditing = YES;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }
}

//选中图片之后执行的方法将图片上传到服务器，上传成功之后直接改变头像
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString*url=[NSString stringWithFormat:@"%@AppUserCenter/PostSaveUserLogo?from=app",SERVER_ADDRESS];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
                NSData*data=UIImageJPEGRepresentation(image, 0.5);
                NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
                formatter.dateFormat = @"yyyyMMddHHmmss";
                NSString *str = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString stringWithFormat:@"%@.jpg",str];
                [formData appendPartWithFileData:data name:@"IMG_PATHS" fileName:fileName mimeType:@"image/jpeg"];
    }
     
          success:^(NSURLSessionDataTask *task, id responseObject) {
              
              headImg.image=image;
              
          } failure:^(NSURLSessionDataTask *task, NSError *error) {
              
          }];

    [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
}



-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [_imagePickerController dismissViewControllerAnimated:YES completion:nil];
}

//返回上一层
-(void)backUp
{
   [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
